<?php

/**
 * Name: 班长审核记录模型层
 * User: Silent
 * Date: 2025-11-07
 * Time: 15:00:00
 */

namespace App\Models\Checkin;

use App\Models\Config\Employee;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class LeaderReview extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'leader_reviews';

    protected $guarded = [];

    protected $casts = [
        'review_date' => 'date:Y-m-d',
        'reviewed_at' => 'datetime:Y-m-d H:i:s',
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
    ];

    protected static $logName = 'leader_reviews';

    // 审核状态常量
    const STATUS_PENDING = 'pending';    // 待审核
    const STATUS_APPROVED = 'approved';  // 正常
    const STATUS_REJECTED = 'rejected';  // 异常

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 关联班长（员工）
     */
    public function leader()
    {
        return $this->belongsTo(Employee::class, 'leader_id');
    }

    /**
     * 获取审核状态文本
     */
    public function getReviewStatusTextAttribute()
    {
        $statusMap = [
            self::STATUS_PENDING => '待审核',
            self::STATUS_APPROVED => '正常',
            self::STATUS_REJECTED => '异常'
        ];

        return $statusMap[$this->review_status] ?? '未知';
    }

    /**
     * 检查是否已审核
     */
    public function isReviewed()
    {
        return $this->review_status !== self::STATUS_PENDING;
    }

    /**
     * 检查是否为正常审核
     */
    public function isApproved()
    {
        return $this->review_status === self::STATUS_APPROVED;
    }

    /**
     * 检查是否为异常审核
     */
    public function isRejected()
    {
        return $this->review_status === self::STATUS_REJECTED;
    }
}

